package br.com.gerador.component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;

import br.com.gerador.icomponent.IGProperty;

public class LabelComponent extends GComponent {
	private JLabel label = null;
	private JTextField text = null;
	
	public LabelComponent() {
		label = new JLabel();
		text = new JTextField();
		text.addKeyListener(textKeyAdapter);
		text.addFocusListener(textFocusAdapter);
		text.setBorder(BorderFactory.createEmptyBorder());
		add(label);
		label.setText("Fabricio Alves");
		validateTree();
		
		addMouseListener(labelEventMouse);
	}
	
	MouseAdapter labelEventMouse = new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			if( e.getClickCount() == 2 ){
				setText(label.getText());
				remove(label);
				add(text);
			}
			super.mouseClicked(e);
			resize();
			repaint();
			validateTree();
		}
		
		public void mousePressed(MouseEvent e) {
			requestFocus();
		};
	};
		
	KeyAdapter textKeyAdapter = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			if( KeyEvent.VK_ESCAPE == e.getKeyCode() ){
				setText(text.getText());
				remove(text);
				add(label);
				validateTree();
				repaint();
			}
			super.keyPressed(e);
		}
	};
	
	FocusAdapter textFocusAdapter = new FocusAdapter() {
		@Override
		public void focusLost(FocusEvent e) {
			setText(text.getText());
			remove(text);
			add(label);
			validateTree();
			repaint();
			super.focusLost(e);
		}
	};
	
	private void setText(String value){
		label.setText(value);
		text.setText(value);
	}
	@Override
	protected int getDefaultW() {
		return 100;
	}
	
	@Override
	protected int getDefaultH() {
		return 30;
	}
	
	public void fontSize(String value){
		label.setFont(new Font(label.getName(), Font.BOLD, Integer.parseInt(value)));
	}

	String[][] propriedades = new String[][]{
		{"ID","","id"},
		{"font-size","12","fontSize"}
	};
	
	@Override
	public String[][] getPropriedades() {
		return propriedades;
	}

	@Override
	public void setPropriedades(String[][] values) {
		this.propriedades = values;
	}	
}
